package com.yili.oa.controller;

import com.github.pagehelper.PageInfo;
import com.yili.oa.entity.TableBase;
import com.yili.oa.models.Admin;
import com.yili.oa.models.UserExchange;
import com.yili.oa.models.UserRecharge;
import com.yili.oa.service.TurnoverService;
import com.yili.oa.vo.request.UserExchangeSearch;
import com.yili.oa.vo.response.UserExchangeRes;
import com.yili.oa.vo.response.UserRechargeRes;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author lxr
 * @version v0.01
 * @date 2019/7/25 0025
 * @email 200890949@qq.com
 */
@Controller
@RequestMapping("/turnover")
public class TurnoverApi {

    @Autowired
    private TurnoverService turnoverService;

    @GetMapping("/userExchange")
    @ResponseBody
    public TableBase getList(UserExchangeSearch searchBase){
        Admin user = (Admin) SecurityUtils.getSubject().getSession().getAttribute("admin");
        List<UserExchange> list = new ArrayList<>();
        if (user.getIsAdmin().equals(1)){
            list = turnoverService.getList(searchBase,null);
        }else {
            list = turnoverService.getList(searchBase,user.getUsername());
        }
        PageInfo pageInfo = new PageInfo<>(list,searchBase.getLimit());

        List<UserExchangeRes> result = new ArrayList<>();
        for (UserExchange userExchange:
             list) {
            UserExchangeRes userExchangeRes = new UserExchangeRes();
            BeanUtils.copyProperties(userExchange,userExchangeRes);
            Date date = new Date(userExchange.getTime());
            userExchangeRes.setTime(date);
            result.add(userExchangeRes);
        }
        TableBase tableBase = new TableBase();
        tableBase.setData(result);
        tableBase.setCode(0);
        tableBase.setMsg("");
        tableBase.setCount(pageInfo.getTotal());
        return tableBase;
    }

    @GetMapping("/userRecharge")
    @ResponseBody
    public TableBase getRechargeList(UserExchangeSearch searchBase){
        Admin user = (Admin) SecurityUtils.getSubject().getSession().getAttribute("admin");
        List<UserRecharge> list = new ArrayList<>();
        if (user.getIsAdmin().equals(1)){
            list = turnoverService.getRechargeList(searchBase,null);
        }else {
            list = turnoverService.getRechargeList(searchBase,user.getUsername());
        }
        PageInfo pageInfo = new PageInfo<>(list,searchBase.getLimit());

        List<UserRechargeRes> result = new ArrayList<>();
        for (UserRecharge userRecharge:
                list) {
            UserRechargeRes userRechargeRes = new UserRechargeRes();
            BeanUtils.copyProperties(userRecharge,userRechargeRes);
            Date date = new Date(userRecharge.getTime());
            userRechargeRes.setDate(date);
            result.add(userRechargeRes);
        }
        TableBase tableBase = new TableBase();
        tableBase.setData(result);
        tableBase.setCode(0);
        tableBase.setMsg("");
        tableBase.setCount(pageInfo.getTotal());
        return tableBase;
    }

    @PostMapping("/update")
    @ResponseBody
    public String upd(@RequestBody UserExchangeSearch userExchangeReq){
        Admin user = (Admin) SecurityUtils.getSubject().getSession().getAttribute("admin");
        if (user==null){
            return "noUser";
        }
        turnoverService.update(userExchangeReq);
        return "success";
    }
}
